<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  <title>View Animation</title>
  <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css"
    crossorigin="anonymous">
  <link rel="stylesheet" type="text/css"
    href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.2/css/fontawesome.min.css"
    crossorigin="anonymous">
  <link rel="stylesheet" type="text/css"
    href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.2/css/solid.css" crossorigin="anonymous">
  <link rel="stylesheet" type="text/css"
    href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.2/css/brands.css" crossorigin="anonymous">
  <link rel="stylesheet" type="text/css" href="./theme/ol.css">
  <link rel="stylesheet" type="text/css" href="/theme/site.css">
  <link rel="icon" type="image/svg+xml" href="/theme/img/logo-light.svg" media="(prefers-color-scheme: light)" />
  <link rel="icon" type="image/svg+xml" href="/theme/img/logo-dark.svg" media="(prefers-color-scheme: dark)" />
</head>

<body>
  <header class="navbar navbar-expand-md navbar-dark mb-3 px-3 py-0 fixed-top" role="navigation">
    <a class="navbar-brand" href="/"><img src="/theme/img/logo-dark.svg" width="70" height="70"
        alt="">&nbsp;OpenLayers</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#olmenu"
      aria-controls="olmenu" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <!-- menu items that get hidden below 768px width -->
    <nav class="collapse navbar-collapse" id="olmenu">
      <ul class="nav navbar-nav ms-auto">
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="docdropdown" role="button" data-bs-toggle="dropdown"
            aria-haspopup="true" aria-expanded="false">Docs</a>
          <div class="dropdown-menu dropdown-menu-end mb-3" aria-labelledby="docdropdown">
            <a class="dropdown-item" href="/doc/">Docs</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="/doc/quickstart.html"><i class="fa fa-check fa-fw me-2 fa-lg"></i>快速入门</a>
            <a class="dropdown-item" href="/doc/faq.html"><i class="fa fa-question fa-fw me-2 fa-lg"></i>常见问题</a>
            <a class="dropdown-item" href="/doc/tutorials/"><i class="fa fa-book fa-fw me-2 fa-lg"></i>教程</a>

          </div>
        </li>
        <li class="nav-item"><a class="nav-link" href="/en/latest/examples/">Examples</a></li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="apidropdown" role="button" data-bs-toggle="dropdown"
            aria-haspopup="true" aria-expanded="false">
            <i class="fa fa-sitemap me-1"></i>API
          </a>
          <div class="dropdown-menu dropdown-menu-end mb-3" aria-labelledby="apidropdown">
            <a class="dropdown-item" href="/en/latest/apidoc/"><i class="fa fa-sitemap fa-fw me-2 fa-lg"></i>v8.2.0
              (latest)</a </div>
        </li>
      </ul>
    </nav>
  </header>

  <div class="container-fluid line-numbers">
    <div id="latest-check" class="alert alert-warning alert-dismissible" role="alert" style="display:none">
      <button id="latest-dismiss" type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      This example uses OpenLayers v<span>8.2.0</span>. The <a id="latest-link" href="#" class="alert-link">latest</a>
      is v<span id="latest-version"></span>.
    </div>


    <div class="row-fluid">
      <a href="#" id="codepen-button" class="btn btn-link float-end">
        <i class="fab fa-codepen fa-lg"></i> Edit
      </a>
      <div class="span12">
        <h4 id="title">View 动画</h4>
        <p class="tags">
          <span class="badge-group">
            <a href="./index.html?q=animation" class="badge badge-info">animation</a><a
              class="badge badge-info tag-modal-toggle" data-bs-toggle="modal" data-bs-target="#tag-example-list"
              data-title="animation" data-content="&lt;ul class=&quot;list-group&quot;&gt;
                  &lt;li&gt;&lt;a class=&quot;list-group-item list-group-item-action&quot; href=&quot;./animated-gif.html&quot;&gt;Animated GIF&lt;/a&gt;
                  &lt;li&gt;&lt;a class=&quot;list-group-item list-group-item-action&quot; href=&quot;./feature-animation.html&quot;&gt;Custom Animation&lt;/a&gt;
                  &lt;li&gt;&lt;a class=&quot;list-group-item list-group-item-action&quot; href=&quot;./flight-animation.html&quot;&gt;Flight Animation&lt;/a&gt;
                  &lt;li&gt;&lt;a class=&quot;list-group-item list-group-item-action&quot; href=&quot;./feature-move-animation.html&quot;&gt;Marker Animation&lt;/a&gt;
                  &lt;li&gt;&lt;a class=&quot;list-group-item list-group-item-action active&quot; href=&quot;./animation.html&quot;&gt;View Animation&lt;/a&gt;
                  &lt;/ul&gt;" tabindex="0">5</a>
          </span>
        </p>
        <div class="modal modal-tag-example" id="tag-example-list" tabindex="-1" role="dialog"
          aria-labelledby="tag-example-title" aria-hidden="true">
          <div class="modal-dialog modal-dialog-scrollable" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="tag-example-title"></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
              </div>
              <div class="modal-body">
                <div class="list-group"></div>
              </div>
            </div>
          </div>
        </div>
        <div id="map" class="map"></div>
        <button id="rotate-left" title="Rotate clockwise">↻</button>
        <button id="rotate-right" title="Rotate counterclockwise">↺</button>
        <button id="pan-to-london">平移至London</button>
        <button id="elastic-to-moscow">拉伸弹跳至Moscow</button>
        <button id="bounce-to-istanbul">弹跳至Istanbul</button>
        <button id="spin-to-rome">旋转至Rome</button>
        <button id="fly-to-bern">飞行至Bern</button>
        <button id="rotate-around-rome">旋转环绕至Rome</button>
        <button id="tour">旅行</button>

      </div>
      <form method="POST" id="codepen-form" target="_blank" action="https://codesandbox.io/api/v1/sandboxes/define">
        <input id="codesandbox-params" type="hidden" name="parameters">
      </form>
    </div>

    <div class="row-fluid">
      <div class="span12">
        <p id="shortdesc">Demonstrates animated pan, zoom, and rotation.</p>
        <div id="docs">
          <p>本示例演示使用<code>view.animate()</code>方法来一个或多个动作。</p>
        </div>
      </div>
    </div>

    <div class="row-fluid">
      <h5 class="source-heading">main.js</h5>
      <pre><code id="example-js-source" class="language-js">import Map from &#x27;ol/Map.js&#x27;;
import OSM from &#x27;ol/source/OSM.js&#x27;;
import TileLayer from &#x27;ol/layer/Tile.js&#x27;;
import View from &#x27;ol/View.js&#x27;;
import {easeIn, easeOut} from &#x27;ol/easing.js&#x27;;
import {fromLonLat} from &#x27;ol/proj.js&#x27;;

const london &#x3D; fromLonLat([-0.12755, 51.507222]);
const moscow &#x3D; fromLonLat([37.6178, 55.7517]);
const istanbul &#x3D; fromLonLat([28.9744, 41.0128]);
const rome &#x3D; fromLonLat([12.5, 41.9]);
const bern &#x3D; fromLonLat([7.4458, 46.95]);

const view &#x3D; new View({
  center: istanbul,
  zoom: 6,
});

const map &#x3D; new Map({
  target: &#x27;map&#x27;,
  layers: [
    new TileLayer({
      preload: 4,
      source: new OSM(),
    }),
  ],
  view: view,
});

// A bounce easing method (from https://github.com/DmitryBaranovskiy/raphael).
function bounce(t) {
  const s &#x3D; 7.5625;
  const p &#x3D; 2.75;
  let l;
  if (t &lt; 1 / p) {
    l &#x3D; s * t * t;
  } else {
    if (t &lt; 2 / p) {
      t -&#x3D; 1.5 / p;
      l &#x3D; s * t * t + 0.75;
    } else {
      if (t &lt; 2.5 / p) {
        t -&#x3D; 2.25 / p;
        l &#x3D; s * t * t + 0.9375;
      } else {
        t -&#x3D; 2.625 / p;
        l &#x3D; s * t * t + 0.984375;
      }
    }
  }
  return l;
}

// An elastic easing method (from https://github.com/DmitryBaranovskiy/raphael).
function elastic(t) {
  return (
    Math.pow(2, -10 * t) * Math.sin(((t - 0.075) * (2 * Math.PI)) / 0.3) + 1
  );
}

function onClick(id, callback) {
  document.getElementById(id).addEventListener(&#x27;click&#x27;, callback);
}

onClick(&#x27;rotate-left&#x27;, function () {
  view.animate({
    rotation: view.getRotation() + Math.PI / 2,
  });
});

onClick(&#x27;rotate-right&#x27;, function () {
  view.animate({
    rotation: view.getRotation() - Math.PI / 2,
  });
});

onClick(&#x27;rotate-around-rome&#x27;, function () {
  // Rotation animation takes the shortest arc, so animate in two parts
  const rotation &#x3D; view.getRotation();
  view.animate(
    {
      rotation: rotation + Math.PI,
      anchor: rome,
      easing: easeIn,
    },
    {
      rotation: rotation + 2 * Math.PI,
      anchor: rome,
      easing: easeOut,
    }
  );
});

onClick(&#x27;pan-to-london&#x27;, function () {
  view.animate({
    center: london,
    duration: 2000,
  });
});

onClick(&#x27;elastic-to-moscow&#x27;, function () {
  view.animate({
    center: moscow,
    duration: 2000,
    easing: elastic,
  });
});

onClick(&#x27;bounce-to-istanbul&#x27;, function () {
  view.animate({
    center: istanbul,
    duration: 2000,
    easing: bounce,
  });
});

onClick(&#x27;spin-to-rome&#x27;, function () {
  // Rotation animation takes the shortest arc, so animate in two parts
  const center &#x3D; view.getCenter();
  view.animate(
    {
      center: [
        center[0] + (rome[0] - center[0]) / 2,
        center[1] + (rome[1] - center[1]) / 2,
      ],
      rotation: Math.PI,
      easing: easeIn,
    },
    {
      center: rome,
      rotation: 2 * Math.PI,
      easing: easeOut,
    }
  );
});

function flyTo(location, done) {
  const duration &#x3D; 2000;
  const zoom &#x3D; view.getZoom();
  let parts &#x3D; 2;
  let called &#x3D; false;
  function callback(complete) {
    --parts;
    if (called) {
      return;
    }
    if (parts &#x3D;&#x3D;&#x3D; 0 || !complete) {
      called &#x3D; true;
      done(complete);
    }
  }
  view.animate(
    {
      center: location,
      duration: duration,
    },
    callback
  );
  view.animate(
    {
      zoom: zoom - 1,
      duration: duration / 2,
    },
    {
      zoom: zoom,
      duration: duration / 2,
    },
    callback
  );
}

onClick(&#x27;fly-to-bern&#x27;, function () {
  flyTo(bern, function () {});
});

function tour() {
  const locations &#x3D; [london, bern, rome, moscow, istanbul];
  let index &#x3D; -1;
  function next(more) {
    if (more) {
      ++index;
      if (index &lt; locations.length) {
        const delay &#x3D; index &#x3D;&#x3D;&#x3D; 0 ? 0 : 750;
        setTimeout(function () {
          flyTo(locations[index], next);
        }, delay);
      } else {
        alert(&#x27;Tour complete&#x27;);
      }
    } else {
      alert(&#x27;Tour cancelled&#x27;);
    }
  }
  next(true);
}

onClick(&#x27;tour&#x27;, tour);
</code></pre>
    </div>

    <div class="row-fluid">
      <h5 class="source-heading">index.html</h5>
      <pre><code id="example-html-source" class="language-markup">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;View Animation&lt;/title&gt;
    &lt;link rel="stylesheet" href="node_modules/ol/ol.css"&gt;
    &lt;style&gt;
      .map {
        width: 100%;
        height: 400px;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id&#x3D;&quot;map&quot; class&#x3D;&quot;map&quot;&gt;&lt;/div&gt;
    &lt;button id&#x3D;&quot;rotate-left&quot; title&#x3D;&quot;Rotate clockwise&quot;&gt;↻&lt;/button&gt;
    &lt;button id&#x3D;&quot;rotate-right&quot; title&#x3D;&quot;Rotate counterclockwise&quot;&gt;↺&lt;/button&gt;
    &lt;button id&#x3D;&quot;pan-to-london&quot;&gt;Pan to London&lt;/button&gt;
    &lt;button id&#x3D;&quot;elastic-to-moscow&quot;&gt;Elastic to Moscow&lt;/button&gt;
    &lt;button id&#x3D;&quot;bounce-to-istanbul&quot;&gt;Bounce to Istanbul&lt;/button&gt;
    &lt;button id&#x3D;&quot;spin-to-rome&quot;&gt;Spin to Rome&lt;/button&gt;
    &lt;button id&#x3D;&quot;fly-to-bern&quot;&gt;Fly to Bern&lt;/button&gt;
    &lt;button id&#x3D;&quot;rotate-around-rome&quot;&gt;Rotate around Rome&lt;/button&gt;
    &lt;button id&#x3D;&quot;tour&quot;&gt;Take a tour&lt;/button&gt;

    &lt;script type="module" src="main.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>
    </div>

    <div class="row-fluid">
      <h5 class="source-heading">package.json</h5>
      <pre><code id="example-pkg-source" class="language-json">{
  &quot;name&quot;: &quot;animation&quot;,
  &quot;dependencies&quot;: {
    &quot;ol&quot;: &quot;8.2.0&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;vite&quot;: &quot;^3.2.3&quot;
  },
  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;vite&quot;,
    &quot;build&quot;: &quot;vite build&quot;
  }
}</code></pre>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"></script>
  <script src="common.js"></script>
  <script src="animation.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/components/prism-core.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/plugins/autoloader/prism-autoloader.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/plugins/toolbar/prism-toolbar.min.js"></script>
  <script
    src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.js"></script>
  <script src="./resources/common.js"></script>
  <script>
    document.getElementById('tag-example-list')
      .addEventListener('show.bs.modal', function (event) {
        const button = event.relatedTarget; // Button that triggered the modal
        this.querySelector('.modal-title').innerText = button.dataset.title;
        this.querySelector('.modal-body').innerHTML = button.dataset.content;
      });
  </script>
  <script>
    const releaseUrl = 'https://cdn.jsdelivr.net/npm/ol/package.json';
    fetch(releaseUrl).then(function (response) {
      return response.json();
    }).then(function (json) {
      const latestVersion = json.version;
      document.getElementById('latest-version').innerHTML = latestVersion;
      const url = window.location.href;
      const branchSearch = url.match(/\/([^\/]*)\/examples\//);
      const storageKey = 'ol-dismissed=-' + latestVersion;
      const dismissed = localStorage.getItem(storageKey) === 'true';
      if (branchSearch && !dismissed && /^v[0-9\.]*$/.test(branchSearch[1]) && '8.2.0' != latestVersion) {
        const link = url.replace(branchSearch[0], '/latest/examples/');
        fetch(link, { method: 'head' }).then(function (response) {
          const a = document.getElementById('latest-link');
          a.href = response.status == 200 ? link : '../../latest/examples/';
        });
        const latestCheck = document.getElementById('latest-check');
        latestCheck.style.display = '';
        document.getElementById('latest-dismiss').onclick = function () {
          latestCheck.style.display = 'none';
          localStorage.setItem(storageKey, 'true');
        }
      }
    });
  </script>
</body>

</html>